<?php

class Order_Model extends CI_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	function getAll($page='', $limit=''){
		
		if(!empty($limit))
		$this->db_front->order_by('date_added', 'desc');
		$this->db_front->limit($limit,$page);
		$query = $this->db_front->get('order');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}function get_store($user_id){
		$this->db_front->where('user_id', $user_id);

		$query = $this->db_front->get('store');
		if ($query->num_rows() == 1) return $query->row();
		return NULL;

	}
	
	function get_order_by_store($store_id='', $page='', $limit=''){
		$query = $this->db_front->query('SELECT * FROM (`order`) WHERE order_id in (select order_id from order_product where store_id = "'.$store_id.'")');
		
		if(!empty($limit))
		$query = $this->db_front->query('SELECT * FROM (`order`) WHERE order_id in (select order_id from order_product where store_id = "'.$store_id.'") limit '.$page.','.$limit.'');
		//echo $this->db_front->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();	
	}

	function get_order_status(){
		$query = $this->db_front->get('order_status');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_by_id($order_id){
        return $this->db_front->get_where('order', array('order_id'=>$order_id))->row();
	}
	
	function get_order_product_by_order($order_id){
		$this->db_front->where('order_id', $order_id);

		$query = $this->db_front->get('order_product');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_total($order_id){
		$this->db_front->where('order_id', $order_id);

		$query = $this->db_front->get('order_total');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function sum_total($order_id){
		$this->db_front->where('order_id', $order_id);
		$this->db_front->select_sum('value');
		$query = $this->db_front->get('order_total');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_order_product_by_store($store_id){
		$this->db_front->where('store_id', $store_id);
		$query = $this->db_front->get('order_product');
			
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
	}
	
	function get_order($order_id){
		$this->db_front->where('order_id', $order_id);

		$query = $this->db_front->get('order');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	

	
	function get_order_product($order_id, $store_id){
		$this->db_front->where('order_id', $order_id);
		$this->db_front->where('store_id', $store_id);
		$query = $this->db_front->get('order_product');
		
		//echo $this->db_front->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function sum_order_product($order_id, $store_id){
		$this->db_front->where('order_id', $order_id);
		$this->db_front->where('store_id', $store_id);
		$this->db_front->select_sum('subtotal');
		$query = $this->db_front->get('order_product');
		
		//echo $this->db_front->last_query();
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function update_status($order_id){
		$data = array(
					'order_status_id' => $this->input->post('order_status_id'),
					'payment_status_id' => $this->input->post('payment_status_id'),
					'date_modified' => date ('Y-m-d H:i:s')
				);
		$this->db_front->where('order_id', $order_id);
        $this->db_front->update('order', $data);
	}
	
	function add_history($order_id){
		$data = array(
					'order_id'		  => $order_id,
					'order_status_id' => $this->input->post('order_status_id'),
					'payment_status_id' => $this->input->post('payment_status_id'),
					'comment' 		  => $this->input->post('comment'),
					'date_added' 	  => date ('Y-m-d H:i:s')
		);
		$this->db_front->where('order_id', $order_id);
        $this->db_front->insert('order_history', $data);
	}
	
	function get_history($order_id, $page='', $limit=''){
		
		$this->db_front->where('order_id', $order_id);
		if(!empty($limit))
		$this->db_front->limit($limit,$page);
		
		$query = $this->db_front->get('order_history');
		
		//echo $this->db_front->last_query();
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}
	}
	
	function get_order_option_by_order_product($order_product_id){
		$this->db_front->where('order_product_id', $order_product_id);
		$query = $this->db_front->get('order_option');
		if ($query->num_rows() > 0) return $query->row();
		return NULL;
	}
	
	function update($order_id, $data){
		$this->db_front->where('order_id', $order_id);
        $this->db_front->update('order', $data);
	}
	
	function get_payment_status(){
		$query = $this->db_front->get('payment_status');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_shipping_by_order($order_id){
		$this->db_front->where('order_id',$order_id);
		$query = $this->db_front->get('order_shipping');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_product_by_id($product_id){
		return $query = $this->db_front->query('select * from product where product_id = "'.$product_id.'"')->row();
	}
	
	function get_status_payment(){
		$query = $this->db_front->get('payment_status');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}

}
